package com.game.spacetrader.data;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.game.spacetrader.Game;
import com.game.spacetrader.R;

/**
 * This class will handle the saving of the game
 */
public class SaveGame  {
	private String returnString;
	
	/**
	 * Sets returnString to "", calls alert()
	 * 
	 * @param mv
	 */
	public void save(Context mv) {
		returnString = "";
		alert(mv);
		
	}
	
	/**
	 * Actually saves the game to a file using an ObjectOutputStream
	 */
	public void createFile(String fileName) {
		if(fileName.length() != 0)
			returnString = fileName;
		if(returnString == "")
			return;
		ObjectOutputStream outputStream = null;
		try {
			File sdcard = Environment.getExternalStorageDirectory();
			File dir = new File (sdcard.getAbsolutePath() + "/Android/com.game.spacetrader");
			File file = new File(dir, returnString + ".stdata");
            outputStream = new ObjectOutputStream(new FileOutputStream(file));
            
            Game game = Game.getInstance();
            
            outputStream.writeObject(game);
        } catch (FileNotFoundException ex) {
        	Log.d(ex.toString(), "test1");
        } catch (IOException ex) {
        	Log.d(ex.toString(), "test2");
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.flush();
                    outputStream.close();
                }
            } catch (IOException ex) {
            	Log.d(ex.toString(), "test3");
            }
        }
	}
	
	/**
	 * Opens an AlertDialog to allow user input for file name
	 * 
	 * @param mv
	 */
	private void alert(Context mv) {
		
		LayoutInflater li = LayoutInflater.from(mv);
		View promptsView = li.inflate(R.layout.savefilealert, null);

		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
				mv);

		alertDialogBuilder.setView(promptsView);

		final EditText userInput = (EditText) promptsView
				.findViewById(R.id.saveFileEditText);

		alertDialogBuilder
			.setCancelable(false)
			.setTitle("Save Game - Enter File Name")
			.setPositiveButton("Save",
			  new DialogInterface.OnClickListener() {
			    public void onClick(DialogInterface dialog,int id) {
			    	returnString = userInput.getText().toString();
			    	SaveGame.this.createFile("");
			    }
			  })
			.setNegativeButton("Cancel",
			  new DialogInterface.OnClickListener() {
			    public void onClick(DialogInterface dialog,int id) {
			    	dialog.cancel();
			    }
			  });

		AlertDialog alertDialog = alertDialogBuilder.create();

		alertDialog.show();
	}
}